package _18_剑指OfferII;

public class _097_剑指OfferII子序列的数目 {

    public static void main(String[] args) {

        _097_剑指OfferII子序列的数目 v = new _097_剑指OfferII子序列的数目();

        String str1 = "rabbbit";
        String str2 = "rabbit";
        System.out.println(v.numDistinct(str1, str2));

    }

    public int numDistinct(String s, String t) {
        if (s.length() < t.length()) return 0;
        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();
        int sLen = sChars.length;
        int tLen = tChars.length;
        int[][] dp = new int[sLen + 1][tLen + 1];
        for (int i = 0; i < sLen; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i <= sLen; i++) {
            for (int j = 1; j <= tLen; j++) {
                if (j > i) continue;
                if (sChars[i - 1] == tChars[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[sLen][tLen];
    }

}
